dojo.provide("lotex.util.Tree");

dojo.declare("lotex.util.Tree", null,
    function() {
        return new lotex.util.TreeNode("__ROOT__");
    },
{
}
);

dojo.declare("lotex.util.TreeNode", null,
    function(obj) {
        this._value = obj;
        this._nodes = [];
        this._parent = null;
    },
{
    addNode: function(node) {
        this._nodes.push(node);
        node.setParent(this);
        return this;
    },

    addChild: function(node) {
        return this.addNode(node);
    },

    addBrother: function(node) {
        return this.getParent().addChild(node);
    },

    haveChildren: function() {
        return this._nodes.length > 0;
    },

    getChildren: function() {
        return this._nodes;
    },

    getParent: function() {
        return this._parent;
    },

    getLastNode: function() {
        return this._lastNode;
    },

    getValue: function() {
        return this._value;
    },

    setParent: function(node) {
        this._parent = node;
    },

    setValue: function(value) {
        this._value = value;
    },

    forEach: function(/* function */ fn, /* object? */ scope){
        //  summary
        //  functional iterator, following the mozilla spec.
        var s=scope||dj_global;
        if(Array.forEach){
            Array.forEach(this._nodes, fn, s);
        }else{
            for(var i=0; i<this._nodes.length; i++){
                fn.call(s, this._nodes[i], i, this._nodes);
            }
        }
    }
}
);

/*
// return ?
lotex.util.Tree = new lotex.util.TreeNode("__ROOT__");

ft = new lotex.util.Tree();
ft.addChild(new lotex.util.TreeNode("1"));

ft.lastNode().parent().addChild();

var lastNode = ft.getLastNode();
var lastSpace = 0;
if(sc>lastSpace) lastNode.addChild();
else if(sc==lastSpace) lastNode.addBrother();
else if(sc<lastSpace) lastNode.getParent().addBrother();

*/


lotex.util.TreeFilePaser = function(content) {
    // TODO \r\n \n??
    var ca = content.split("\r\n");

    var ladder = [-1];
    var sc;
    var ft = new lotex.util.TreeNode("_ROOT_");

    var lastNode = ft;

    for(var i=0;i<ca.length;i++) {
        sc = prespace(ca[i]);

        var selItem = {
            space: sc,
            content: ca[i]
        };
        var si = new lotex.util.TreeNode(ca[i]);
        //var si = new lotex.util.TreeNode(selItem);

        var lastSpace = ladder[ladder.length-1];

        if(sc>lastSpace) {
            lastNode.addChild(si);
            ladder.push(sc);
        } else if(sc==lastSpace) lastNode.addBrother(si);
        else if(sc<lastSpace) {
            var siRoot = lastNode;
            while (sc < ladder[ladder.length-1]) {
                ladder.pop();
                siRoot = siRoot.getParent();
                var dc = ladder[ladder.length-1];
//d("Tree.js-dc",dc);
            }
            siRoot.addBrother(si);
        }

        lastNode = si;
    }
//d("Tree.js","TreeFilePaser OK!");

    return ft;
}
